#coding=utf-8

import tkinter as tk

import easygui as g

import random

import webbrowser

import os

import sys

# Check

def check(file_path):
	if (os.access(file_path,os.F_OK)):
		return False
	return True

check_list = []

if (check("Photos/1-X.gif")):
	check_list.append("1-X.gif")

if (check("Photos/1-Y.gif")):
	check_list.append("1-Y.gif")

if (check("Photos/X.gif")):
	check_list.append("X.gif")

if (check("Photos/XX.gif")):
	check_list.append("XX.gif")

if (check("Photos/XY.gif")):
	check_list.append("XY.gif")

if (len(check_list) != 0):
	error_report = "启动失败\n缺失文件(Photos文件下)\n"
	for i in check_list:
		error_report += i
		error_report += "\n"
	g.msgbox(error_report,"性别遗传模拟程序","关闭")
	sys.exit(1)

# tkinter

# base config

windows = tk.Tk()

windows.title("性别遗传模拟程序")

windows.geometry("400x400")

windows.resizable(False,False)

# var

# free_female_var = tk.StringVar()

# free_male_var = tk.StringVar()

free_merge_var = tk.StringVar()

male_nums = tk.StringVar()

female_nums = tk.StringVar()

male_var_nums = 0

female_var_nums = 0

female_var = ""

male_var = ""

data = []

# Image Var

_X_1 = tk.PhotoImage(file = "Photos/1-X.gif")
_Y_1 = tk.PhotoImage(file = "Photos/1-Y.gif")
_X = tk.PhotoImage(file = "Photos/X.gif")
_XX = tk.PhotoImage(file = "Photos/XX.gif")
_XY = tk.PhotoImage(file = "Photos/XY.gif")

# Image

Free_female_result_photo = tk.Label(windows,font = 20)

Free_female_result_photo.place(x = 55,y = 110)

Free_male_result_photo = tk.Label(windows,font = 20)

Free_male_result_photo.place(x = 298,y = 110)

Free_merge_result_photo = tk.Label(windows,font=20)

Free_merge_result_photo.place(x = 180,y= 110)

# Menu - function

def fast_test():
	# This is a function of fast test
	global female_var_nums
	global male_var_nums
	global data

	sub_nums = g.integerbox(msg="样本数量(1~1000000)",title="性别遗传模拟程序",lowerbound=1,upperbound=1000000)

	for i in range(0,sub_nums):
		if(round(random.random()) == 0):
			female_var_nums += 1
			data.append("XX");
		else:
			male_var_nums += 1
			data.append("XY")

	male_nums.set(str(male_var_nums))

	female_nums.set(str(female_var_nums))

def reset_value():

	# free_female_var.set("")

	# free_male_var.set("")

	Free_female_result_photo.config(image="")

	Free_male_result_photo.config(image="")

	Free_merge_result_photo.config(image="")

	male_nums.set("0")

	female_nums.set("0")

	global female_var
	global male_var
	global female_var_nums
	global male_var_nums
	global data

	female_var = ""
	male_var = ""
	female_var_nums = 0
	male_var_nums = 0
	data = []

def out_data():
	global female_var_nums
	global male_var_nums
	global data

	if (female_var_nums + male_var_nums == 0):
		g.msgbox("没有数据","性别遗传模拟程序","返回")
		return ;

	saved = g.filesavebox(title="性别遗传模拟程序",default="data.csv",filetypes="csv")

	f = open(saved,"w")

	# res = "男性(XY)," + str(male_var_nums) + ",\n女性(XX)," + str(female_var_nums) + ",\n序号,性染色体,性别,\n"
	res = ""

	res += "男性(XY)," + str(male_var_nums) + "," + str(round(male_var_nums/(female_var_nums + male_var_nums)*100,2)) + "%,\n"
	res += "女性(XY)," + str(female_var_nums) + "," + str(round(female_var_nums/(female_var_nums + male_var_nums)*100,2)) + "%,\n"

	f.write(res)

	id = 1

	for i in data:
		res = str(id) + "," + i + ","
		if (i == "XX"):
			res += "女性,\n"
		else:
			res += "男性,\n"
		f.write(res)
		id += 1

	f.close()

def help_docs():
	# os.system("start https://gitee.com/zcmain/gender_genetic_simulation/wikis/")
	webbrowser.open("https://gitee.com/zcmain/gender_genetic_simulation/wikis/")

def about():
	g.msgbox("性别遗传模拟程序\n作者:zcmain\n开源地址:https://gitee.com/zcmain/gender_genetic_simulation","性别遗传模拟程序","返回")

def commit_bug():
	# os.system("start https://gitee.com/zcmain/gender_genetic_simulation/issues/I1G93F")
	webbrowser.open("https://gitee.com/zcmain/gender_genetic_simulation/issues/I1G93F")

def commit_advice():
	# os.system("start https://gitee.com/zcmain/gender_genetic_simulation/issues/I1G93H")
	webbrowser.open("https://gitee.com/zcmain/gender_genetic_simulation/issues/I1G93H")

def data_result():
	global female_var_nums
	global male_var_nums

	if (female_var_nums + male_var_nums == 0):
		g.msgbox("没有数据","性别遗传模拟程序","返回")
		return ;

	res = "总人数:" + str(male_var_nums + female_var_nums) + "\n"
	res += "男性(XY):" + str(male_var_nums) + " " + str(round(male_var_nums/(female_var_nums + male_var_nums)*100,2)) + "%\n"
	res += "女性(XY):" + str(female_var_nums) + " " + str(round(female_var_nums/(female_var_nums + male_var_nums)*100,2)) + "%\n"
	g.msgbox(res,"性别遗传模拟程序","返回")

# Main Function

def merge():

	Free_male_result_photo.config(image="")

	Free_female_result_photo.config(image="")

	# free_female_var.set("")

	# free_male_var.set("")

	free_merge_var.set("")

	global female_var
	global male_var
	global female_var_nums
	global male_var_nums
	global data

	if (female_var == "X" and male_var == "X"):
		Free_merge_result_photo.config(image=_XX)
		free_merge_var.set("女性")
		data.append("XX")
		female_var_nums += 1
		female_var = ""
		male_var = ""
	if (female_var == "X" and male_var == "Y"):
		Free_merge_result_photo.config(image=_XY)
		free_merge_var.set("男性")
		data.append("XY")
		male_var_nums += 1
		female_var = ""
		male_var = ""

	male_nums.set(str(male_var_nums))
	female_nums.set(str(female_var_nums))


def female_free():

	free_merge_var.set("")

	global female_var

	# free_female_var.set("X")

	Free_merge_result_photo.config(image="")

	Free_female_result_photo.config(image=_X)

	female_var = "X"

def male_free():
	# Using round

	free_merge_var.set("")

	Free_merge_result_photo.config(image="")

	global male_var

	if (round(random.random()) == 0):
		# free_male_var.set("X")
		Free_male_result_photo.config(image=_X_1)
		male_var = "X"
	else:
		# free_male_var.set("Y")
		Free_male_result_photo.config(image=_Y_1)
		male_var = "Y"

# Menu

bar = tk.Menu(windows)

command = tk.Menu(bar, tearoff = 0)

helper = tk.Menu(bar, tearoff = 0)

bar.add_cascade(label = "命令",menu = command)

bar.add_cascade(label = "帮助",menu = helper)

# Menu - command

command.add_command(label = "快速模拟", command = fast_test)

command.add_command(label = "复位",command = reset_value)

command.add_command(label = "导出数据",command = out_data)

command.add_command(label = "数据分析",command = data_result)

command.add_command(label = "退出",command = windows.quit)

# Menu - help

helper.add_command(label = "帮助文档",command = help_docs)

helper.add_command(label = "提交Bug",command = commit_bug)

helper.add_command(label = "提交建议",command = commit_advice)

helper.add_command(label = "关于性别遗传模拟程序",command = about)

# Put Menu

windows.config(menu = bar)

# Label

female = tk.Label(windows,text = "卵细胞",font =20)

female.place(x = 50,y = 50)

male = tk.Label(windows,text= "精子",font = 20)

male.place(x = 300,y = 50)

# Free_female_result = tk.Label(windows,textvariable = free_female_var,font = 20)

# Free_female_result.place(x = 75,y = 125)

# Free_male_result = tk.Label(windows,textvariable = free_male_var,font = 20)

# Free_male_result.place(x = 315,y = 125)

Free_merge_result = tk.Label(windows,textvariable = free_merge_var,font = 20)

Free_merge_result.place(x = 180,y = 50)

Male_Table = tk.Label(windows,text="男性:",font=20)

Male_Table.place(x = 0,y = 300)

Male_Free = tk.Label(windows,textvariable=male_nums,font=20)

Male_Free.place(x = 50,y = 300)

Female_Table = tk.Label(windows,text="女性:",font=20)

Female_Table.place(x = 0,y = 330)

Female_Free = tk.Label(windows,textvariable=female_nums,font=20)

Female_Free.place(x = 50,y = 330)

# free_female_var.set("")

# free_male_var.set("")

free_merge_var.set("")

male_nums.set("0")

female_nums.set("0")

# Button

Free_female = tk.Button(windows,text = "排出",font = 20,command = female_free)

Free_female.place(x = 55,y = 200)

Free_male = tk.Button(windows,text = "排出",font = 20,command = male_free)

Free_male.place(x = 295,y = 200)

Merge_both = tk.Button(windows,text = "精卵结合",font = 20,command = merge)

Merge_both.place(x = 156,y = 200)

# MainLoop

windows.mainloop()